package com.witmore.neutron.ai.cognitive.exception;

/**
 * 会话次数使用相关异常
 * Author: 豆子高
 * Date: 2025/1/15
 */
public class SessionUsageException extends RuntimeException {

    private final String errorCode;
    private final Integer userId;
    private final Integer remainingCount;

    public SessionUsageException(String errorCode, String message, Integer userId, Integer remainingCount) {
        super(message);
        this.errorCode = errorCode;
        this.userId = userId;
        this.remainingCount = remainingCount;
    }

    public SessionUsageException(String errorCode, String message, Integer userId, Integer remainingCount, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode;
        this.userId = userId;
        this.remainingCount = remainingCount;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public Integer getUserId() {
        return userId;
    }

    public Integer getRemainingCount() {
        return remainingCount;
    }

    /**
     * 会话次数不足异常
     */
    public static class InsufficientSessionException extends SessionUsageException {
        public InsufficientSessionException(Integer userId, Integer remainingCount, Integer requiredCount) {
            super("INSUFFICIENT_SESSION", 
                String.format("会话次数不足，当前剩余: %d, 需要: %d", remainingCount, requiredCount), 
                userId, remainingCount);
        }
    }

    /**
     * 用户不存在异常
     */
    public static class UserNotFoundException extends SessionUsageException {
        public UserNotFoundException(Integer userId) {
            super("USER_NOT_FOUND", 
                String.format("用户不存在，用户ID: %d", userId), 
                userId, 0);
        }
    }

    /**
     * 会话次数扣减失败异常
     */
    public static class DeductionFailedException extends SessionUsageException {
        public DeductionFailedException(Integer userId, Integer remainingCount, Throwable cause) {
            super("DEDUCTION_FAILED", 
                String.format("会话次数扣减失败，用户ID: %d, 剩余次数: %d", userId, remainingCount), 
                userId, remainingCount, cause);
        }
    }
}